// pages/photoWall/photoWall.js
import { scrollToLeftEnd } from '../../utils/behavior';
const app = getApp();
Page({
    behaviors: [scrollToLeftEnd],
    /**
     * 页面的初始数据
     */
    data: {
        ipxBottom: 0,
        current: 0,
        //上传照片
        sceneImageArr: [],
        // 图片描述
        description: [],
        // 发布人
        userArr: [],
        userIdArr: [],
        addressArr: [],
        // 发布时间
        timeArr: [],
        idArr: [],
        // 当前描述索引
        currentPicIndex: 0,
        cityflg: '00', //查城市照片墙填  00
        cityname: '', //城市名称
        personalflg: '', //查个人页照片墙填 00
        refresh: true,
        // 缺省显示
        noInfo: false,
        fromWhere: '', //00我的页面照片墙 01秘书团页面
        pageNum: 0,
        loading: true,
        loadingAll: false,
        pageNumCheck: 0,
        loadingCheck: true,
        loadingCheckAll: false,
        operationRecord: [], //打卡记录
    },
    addImgTap(e) {
        wx.navigateTo({
            url: `../../releasePage/pages/addPictures/addPictures?fromWhere=${this.data.fromWhere}`,
        });
    },
    imgPreviewTap(e) {
        this.setData({
            currentPicIndex: e.currentTarget.dataset.current,
        });
        wx.navigateTo({
            url: `../checkIn/imgPreview/imgPreview?fromWhere=${this.data.fromWhere}`,
        });
    },
    jumpInfotap(e) {
        wx.navigateTo({
            url: `../../functionPage/pages/minePage/checkInIfo/checkInIfo?id=${e.currentTarget.dataset.id}`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 34,
            });
        }
        this.setData({
            cityflg: options.cityflg,
            cityname: options.cityname,
            personalflg: options.personalflg,
            fromWhere: options.fromWhere,
        });
        wx.setNavigationBarTitle({
            title: options.title || app.globalData.addressBack,
        });
        this.getPhotoWallList();
        this.getRecords();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    refresh() {
        this.setData({
            sceneImageArr: [],
            description: [],
            userArr: [],
            addressArr: [],
            timeArr: [],
            idArr: [],
            userIdArr: [],
            pageNum: 0,
            loading: true,
            loadingAll: false,
            refresh: false,
            pageNumCheck: 0,
            loadingCheck: true,
            loadingCheckAll: false,
        });
        this.getPhotoWallList();
        this.getRecords();
    },
    //切换nav
    changeNav(e) {
        this.setData({
            current: e.currentTarget.dataset.index,
        });
    },
    //swiper切换
    swiperChange(e) {
        this.setData({
            current: e.detail.current,
        });
    },
    // 获取图片墙
    getPhotoWallList(e) {
        let _that = this;
        if (!_that.data.loadingAll) {
            _that.setData({
                pageNum: _that.data.pageNum + 1,
                loading: true,
                loadingAll: false,
            });
            wx.request({
                url: app.globalData.rootPath + `cjMap/getPhotoWallList`,
                method: 'GET',
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                },
                data: {
                    authId: app.globalData.authId,
                    country: app.globalData.countryBack,
                    cityflg: _that.data.cityflg || '', //查城市照片墙填  00
                    cityname: _that.data.cityname || '', //城市名称
                    personalflg: _that.data.personalflg || '', //查个人页照片墙填 00
                    pageSize: 10,
                    pageNo: _that.data.pageNum,
                },
                success: function (res) {
                    console.log(res);
                    if (res.data.status == 0) {
                        for (let i = 0; i < res.data.data.length; i++) {
                            _that.data.sceneImageArr.push(
                                res.data.data[i].picurl
                            );
                            _that.data.description.push(
                                res.data.data[i].picdescription
                            );
                            _that.data.userArr.push(
                                res.data.data[i].createuname
                            );
                            _that.data.addressArr.push(
                                res.data.data[i].cityname
                            );
                            let checkinTime = app.timeago(
                                res.data.data[i].checkintime ||
                                    res.data.data[i].createtime
                            );
                            _that.data.timeArr.push(checkinTime);
                            _that.data.idArr.push(res.data.data[i].id);
                            _that.data.userIdArr.push(
                                res.data.data[i].createuid
                            );
                        }
                        _that.setData({
                            sceneImageArr: _that.data.sceneImageArr,
                            description: _that.data.description,
                            userArr: _that.data.userArr,
                            timeArr: _that.data.timeArr,
                            idArr: _that.data.idArr,
                            addressArr: _that.data.addressArr,
                            userIdArr: _that.data.userIdArr,
                            loading: false,
                        });
                        if (res.data.data.length < 10) {
                            _that.setData({
                                loadingAll: true,
                            });
                        }
                        if (!_that.data.sceneImageArr.length) {
                            _that.setData({
                                noInfo: true,
                                loading: false,
                                loadingAll: false,
                            });
                        } else {
                            _that.setData({
                                noInfo: false,
                            });
                        }
                    } else {
                        if (res.data.errmsg == '输入页码超出范围') {
                            _that.setData({
                                loadingAll: true,
                                loading: false,
                            });
                        }
                    }
                },
            });
        }
    },
    // 城市打卡记录
    getRecords() {
        let _that = this;
        if (!_that.data.loadingCheckAll) {
            _that.setData({
                pageNumCheck: _that.data.pageNumCheck + 1,
                loadingCheck: true,
                loadingCheckAll: false,
            });
            wx.request({
                url: app.globalData.rootPath + 'cjMap/getCheckinList',
                data: {
                    authId: app.globalData.authId,
                    checkintimeorder: 'desc',
                    country: app.globalData.countryBack,
                    cityname: app.globalData.addressBack,
                    pageSize: 15,
                    pageNo: _that.data.pageNumCheck,
                },
                success(res) {
                    if (res.data.status == 0) {
                        console.log(res);
                        if (
                            _that.data.operationRecord.length <
                            res.data.pager.totalRows
                        ) {
                            if (res.data.data.length > 0) {
                                for (let i = 0; i < res.data.data.length; i++) {
                                    res.data.data[i].createtime = app.timeago(
                                        res.data.data[i].createtime
                                    );
                                    let picurlArr = res.data.data[i].picurl;
                                    let title = '';
                                    let url = '';
                                    picurlArr = picurlArr
                                        ? JSON.parse(picurlArr)
                                        : [];
                                    res.data.data[i].picurl = picurlArr;
                                    _that.data.operationRecord.push(
                                        res.data.data[i]
                                    );
                                }
                            }
                            _that.setData({
                                operationRecord: _that.data.operationRecord,
                                loadingCheck: false,
                            });
                            if (res.data.data.length < 15) {
                                _that.setData({
                                    loadingCheckAll: true,
                                });
                            }
                        } else {
                            _that.setData({
                                loadingCheckAll: true,
                                loadingCheck: false,
                            });
                        }
                    }
                },
            });
        }
    },
    previewImgTap(e) {
        wx.previewImage({
            current: e.currentTarget.dataset.srcarr[0],
            urls: e.currentTarget.dataset.srcarr,
        });
    },
});
